#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
样本管理系统启动脚本
"""

import sys
import os
import logging
from pathlib import Path
from PyQt6.QtWidgets import QApplication
from PyQt6.QtCore import Qt

# 添加项目根目录和src目录到Python路径
project_root = Path(__file__).parent.parent
project_root = project_root.resolve()

# 将项目根目录添加到Python路径
if str(project_root) not in sys.path:
    sys.path.insert(0, str(project_root))

# 将src目录添加到Python路径
src_path = project_root / "src"
if str(src_path) not in sys.path:
    sys.path.insert(0, str(src_path))

# 配置日志
log_file = project_root / "data" / "sample_management.log"
log_file.parent.mkdir(exist_ok=True)

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler(log_file, encoding='utf-8'),
        logging.StreamHandler()
    ]
)

def main():
    """主函数"""
    try:
        # 创建应用程序
        app = QApplication(sys.argv)
        
        # 设置应用程序信息
        app.setApplicationName("小苏科技-LIS样本管理系统")
        app.setApplicationVersion("3.0.0")
        app.setOrganizationName("LIS系统")
        
        # 设置应用程序图标（如果有的话）
        # app.setWindowIcon(QIcon("icon.png"))
        
        # 导入主窗口 - 使用绝对导入
        try:
            from src.ui.main_window import SampleManagementSystem
        except ImportError:
            # 如果绝对导入失败，尝试相对导入
            from ui.main_window import SampleManagementSystem
        
        # 创建主窗口
        window = SampleManagementSystem()
        window.show()
        
        # 运行应用程序
        sys.exit(app.exec())
        
    except Exception as e:
        logging.error(f"应用程序启动失败: {e}")
        print(f"错误: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)

if __name__ == "__main__":
    main() 